;;;
;;; JaCoLiB
;;; Copyright (C) 2008 Alessandro Serra
;;; 
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;; 
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;; 
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;;

;;;; Created on 2008-07-02 10:44:17

(in-package :jacolib)

;
; Shell RPC Server
;

(defclass shell-server (rpc-server)
  ((shell
    :TYPE shell
    :INITARG :shell
    :INITFORM *cl-shell*
    :ACCESSOR shell-server-shell
    :DOCUMENTATION "The shell.")
   (callback-shell
    :TYPE remote-shell
    :INITARG :callback-shell
    :READER shell-server-callback-shell))
  (:documentation "A RPC server for a shell."))

; variables

(defvar *callback-shell* nil "The current remote shell that can be user as callback shell.")

; initializer

(defmethod initialize-instance :after ((server shell-server) &rest initargs)
  (declare (ignore initargs))
  (shell-server-initialize-invoker server))

(defun shell-server-initialize-invoker (server)
  (labels ((reg (method fn)
             (rpc-server-register-method-handler
              server
              method
              fn)))
    (reg "shell-hello"
         (make-rpc-method-handler ()
                                  (shell-hello (shell-server-shell server))))
    (reg "shell-language-name"
         (make-rpc-method-handler ()
                                  (shell-language-name (shell-server-shell server))))
    (reg "shell-get"
         (make-rpc-method-handler ((key 'string))
                                  (shell-get (shell-server-shell server) key)))
    (reg "shell-unset"
         (make-rpc-method-handler ((key 'string))
                                  (shell-unset (shell-server-shell server) key)))
    (reg "shell-set"
         (make-rpc-method-handler ((key 'string) (obj t))
                                  (shell-set (shell-server-shell server) key obj)))
    (reg "shell-eval"
         (make-rpc-method-handler ((code 'string))
                                  (let ((*callback-shell* (shell-server-callback-shell server)))
                                    (shell-eval (shell-server-shell server) code))))
    (reg "shell-source"
         (make-rpc-method-handler ((source 'string))
                                  (let ((*callback-shell* (shell-server-callback-shell server)))
                                    (shell-unset (shell-server-shell server) source))))))

(defun shell-server-quit (shell)
  (rpc-server-quit shell))

(defmethod rpc-server-quit% ((server shell-server))
  (setf (slot-value server 'shell) nil)
  (setf (slot-value server 'callback-shell) nil)
  (call-next-method))
